package stage1.operation.module1;

import java.util.Arrays;
import java.util.Scanner;

public class Backgammon {

    public static void main(String args[]) {
        String[][] map = checkerboardParameters();
        initializeBoard(map);
        
        playChess("b", map);
    }

    // b 代表black是黑子，w代表white是白子
    public static void playChess(String chessPlayer, String[][] map) {
        Scanner sc = new Scanner(System.in);
        String chessColor = (chessPlayer == "b") ? "黑" : "白";
        System.out.println("请" + chessColor + "方落子(格式：行号 列号):");
        try {
            int x = sc.nextInt() + 1;
            int y = sc.nextInt() + 1;
            System.out.println(map[x][y] == null);
            if (map[x][y] != null) {
                System.out.println("此位置已有棋子，请重新输入");
                playChess(chessPlayer, map);
            }
            map[x][y] = chessPlayer;
            initializeBoard(map);

            if (win(map)) {
                sc.close();
                System.out.println(chessColor + "子获胜");
            } else {
                playChess((chessPlayer == "b") ? "w" : "b", map);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("请按规则进行游戏！");
            playChess(chessPlayer, map);
        }
    }

    // 初始化棋盘
    public static void initializeBoard(String[][] map) {
        String[] chessColors = {"w", "b"};
        for (int i = 0; i < 17; i++) {
            for (int j = 0; j < 17; j++) {
                if (i == 0 && j == 0) {
                    System.out.print(" ");
                } else if (i == 0 || j == 0) {
                    System.out.print(map[i][j]);
                } else {
                    if (Arrays.asList(chessColors).contains(map[i][j])) {
                        System.out.print(map[i][j]);
                    } else {
                        System.out.print("+");
                    }
                }
                System.out.print("  ");
            }
            System.out.println();
        }
    }

    public static boolean win(String[][] map) {
        for (int i = 1; i < 12; i++) {
            for (int j = 1; j < 12; j++) {
                String chessColor = map[i][j];
                // 这块代码应该可以封装循环处理的，后续处理
                if (map[i][j] != null &&
                        (
                                (
                                        map[i][j + 1] == chessColor && map[i][j + 2] == chessColor
                                                && map[i][j + 3] == chessColor && map[i][j + 4] == chessColor
                                ) || (
                                        map[i + 1][j] == chessColor && map[i + 2][j] == chessColor
                                                && map[i + 3][j] == chessColor && map[i + 4][j] == chessColor
                                ) || (
                                        map[i + 1][j + 1] == chessColor && map[i + 2][j + 2] == chessColor
                                                && map[i + 3][j + 3] == chessColor && map[i + 4][j + 4] == chessColor
                                ) || (
                                        map[i + 1][j - 1] == chessColor && map[i + 2][j - 2] == chessColor
                                                && map[i + 3][j - 3] == chessColor && map[i + 4][j - 4] == chessColor
                                )
                        )) {
                    return true;
                }
            }
        }
        return false;
    }

    // 棋盘尺寸
    public static String[][] checkerboardParameters() {
        String[][] map = new String[17][17];
        for (int i = 0; i < 17; i++) {
            map[i][0] = map[0][i] = getPiece(i - 1);
        }
        return map;
    }

    // 棋盘尺寸特殊处理
    public static String getPiece(int column) {
        if (column < 10) {
            return String.valueOf(column);
        }
        switch (column) {
            case 10:
                return "a";
            case 11:
                return "b";
            case 12:
                return "c";
            case 13:
                return "d";
            case 14:
                return "e";
            case 15:
                return "f";
            default:
                return "";
        }
    }
}
